<template>
	<view>
		<scroll-view scroll-y class="page">
			<!-- 头部 -->
			<cu-custom bgColor="bg-rgb" :isBack="true">
				<block slot="backText">返回</block>
				<block slot="content">确定入库</block>
				<block slot="right"><view @tap="handleRightClick">批量扫描</view></block>
			</cu-custom>
			<!-- 数据列表 -->
			<view class="list">
				<view class="card animation-slide-bottom" v-for="(item,index) in list" :key="index" :style="[{animationDelay: (index + 1)*0.1 + 's'}]">
					<view class="top">
						<view class="left">移动编号：<text>{{item.mobileCode}}</text></view>
						<view class="right">{{item.mobileType}}</view>
					</view>
					<view class="line"></view>
					<view class="bottom">
						<view class="box">
							<view class="lable">
								料号:
							</view>
							<view class="content">
								{{item.materielCode}}
							</view>
						</view>
						<view class="box">
							<view class="lable">
								客户料号:
							</view>
							<view class="content">
								{{item.clientCode}}
							</view>
						</view>
						<view class="box">
							<view class="lable">
								名称:
							</view>
							<view class="content">
								{{item.materielName}}
							</view>
						</view>
						<view class="box">
							<view class="lable">
								规格:
							</view>
							<view class="content">
								{{item.materielGauge}}
							</view>
						</view>
						<!-- <view class="box">
							<view class="lable">
								预留编号:
							</view>
							<view class="content">
								{{item.reserveCode}}
							</view>
						</view> -->
						<view class="box">
							<view class="lable">
								位置:
							</view>
							<view class="content">
								{{item.storageSite}}
							</view>
						</view>
						<view class="box">
							<view class="lable">
								总数量:
							</view>
							<!-- <input value="" type="number" v-model="myFormData[index].inputNum" /> -->
							<view class="content">
								{{item.inputNum}}
							</view>
						</view>
						<view class="box">
							<view class="lable">
								未入库数量:
							</view>
							<view class="content">
								{{item.unstorageNum}}
							</view>
						</view>
						<view class="box">
							<view class="lable">
								单位:
							</view>
							<view class="content">
								{{item.inputUnit}}
							</view>
						</view>
						<view class="box">
							<view class="lable">
								工厂:
							</view>
							<view class="uni-list-cell-db">
								<picker @change="bindPickerChange($event,index)" range-key="name" :value="indexs" :range="array">
									<view class="uni-input">{{array[indexs].name}}</view>
								</picker>
							</view>
						</view>
						<view class="box">
							<view class="lable">
								位置:
							</view>
							<view class="content">
								{{myFormData[index].storageSite}}
							</view>
						</view>
					</view>
					<view class="line"></view>
					<view class="btns" v-if="item.mobileType != '入库' && item.mobileType != '退货'">
						<view class="btn1" @click="Warehousing('退货',index,item)">退货</view>
						<!-- <view class="btn2" @click="Warehousing('入库',index,item)">入库</view> -->
						<!-- <view class="btn3" @click="WarehousingSaomiao('扫描入库',index,item)">扫描入库</view> -->
					</view>
				</view>
			</view>
			<view class="none" v-if="noDataAvailable">
				<view class="none1">
					暂无数据
				</view>
			</view>
			<view class="none2" v-if="noMoreDataAvailable">
				没有更多数据
			</view>
		</scroll-view>
	</view>
</template>

<script>
	export default {
		name: 'warehouseDetail',
		data() {
			return {
				list: [], // 凭证抬头项目
				lists: [], // 凭证抬头项目
				pageNo: 1, // 上拉加载分页
				noDataAvailable: false, // 暂无数据
				noMoreDataAvailable: false, // 没有更多数据
				id: '', // 物料凭证抬头 ID
				myFormData: [],
				array: [],
				indexs: 0
			}
		},
		onShow(e) {
			this.array = [];
			this.list = []
			this.id = uni.getStorageSync('warehouseId');
			this.getFactory(); // 获取工厂信息

		},
		onPullDownRefresh: function() {
			let _this = this
			_this.pageNo = 1
			_this.list = []
			setTimeout(function() {
				uni.stopPullDownRefresh();
				_this.getListMesCertificateItemByMainId()
			}, 1000);
		},
		onReachBottom: function() {
			let _this = this
			if (this.noDataAvailable == true || this.noMoreDataAvailable == true) {
				return
			}
			_this.pageNo++
			_this.getListMesCertificateItemByMainId()
		},
		methods: {
			// 物料项目
			getListMesCertificateItemByMainId() {
				uni.showLoading({
					mask: true,
					title: '加载中.....'
				})
				let data = {
					pageNo: this.pageNo,
					pageSize: 10,
					perkId: this.id,
					ifInspect: '是',
					ifStorage: '否'
				}
				this.$http.get('storage/mesCertificatePerk/listCertificateItemByPerkId', {
					params: data
				}).then(res => {
					uni.hideLoading()
					if (res.data.success) {
						this.lists = this.list.concat(res.data.result.records);
						for (let i = 0; i < this.lists.length; i++) {
							let data = {
								id: this.lists[i].id,
								factory: this.array[0].facNameone,
								facCode: this.array[0].facCode,
								storageSite: this.array[0].storeName,
								storeCode: this.array[0].storeCode,
								inputNum: this.lists[i].unstorageNum,
							}
							this.myFormData.push(data)
						}
						this.list = this.lists
						if (this.list.length == 0) {
							this.noDataAvailable = true;
						} else if (this.list.length > 0 && res.data.result.records.length == 0) {
							this.noMoreDataAvailable = true;
						} else if (res.data.result.total <= this.pageNo * 10) {
							this.noMoreDataAvailable = true;
						}
					} else {
						// this.$tip.error(res.data.message)
            this.modal.show({ type:'error', content:res.data.message })
					}
				})
			},
			// 工厂查询
			getFactory() {
				let data = {
					pageNo: 1,
					pageSize: 9999
				}
				this.$http.get('organize/mesOrganizeFactory/listMesFactoryStoresite', {
					params: data
				}).then(res => {
					if (res.data.success) {
						for (let s = 0; s < res.data.result.length; s++) {
							let data = {
								name: res.data.result[s].facNameone + '(' + res.data.result[s].storeName + ')',
								facCode: res.data.result[s].facCode,
								facNameone: res.data.result[s].facNameone,
								storeName: res.data.result[s].storeName,
								storeCode: res.data.result[s].storeCode,
							}
							this.array.push(data)
						}
						this.getListMesCertificateItemByMainId(); // 获取列表
					} else {
						// this.$tip.error(res.data.message)
            this.modal.show({ type:'error', content:res.data.message })
					}
				})
			},
			// 选择工厂
			bindPickerChange(e, myFormDataIndex) {
				this.indexs = e.target.value
				this.myFormData[myFormDataIndex].factory = this.array[this.indexs].facNameone
				this.myFormData[myFormDataIndex].facCode = this.array[this.indexs].facCode
				this.myFormData[myFormDataIndex].storageSite = this.array[this.indexs].storeName
				this.myFormData[myFormDataIndex].storeCode = this.array[this.indexs].storeCode
			},
			// 退货 + 入库
			Warehousing(type, index,item) {
				this.$Router.push({
				  name: 'disqualification',
				  params:{
				    objMsg:item,
					type:'退货'
				  }
				})
				// uni.showLoading({
				// 	title: type + '中',
				// 	mask: true
				// })

				// let data = {}
				// if (type == '退货') {
				// 	data = {
				// 		perkId: item.perkId,
				// 		materielCode: item.materielCode,
				// 		factoryName: this.myFormData[index].factory,
				// 		factoryCode:this.myFormData[index].facCode,
				// 		storageSite: this.myFormData[index].storageSite,
				// 		inputNum: this.myFormData[index].inputNum,
				// 		id: this.myFormData[index].id,
				// 		mobileCode: '104',
				// 	}
				// } else if (type == '入库') {
				// 	data = {
				// 		perkId: item.perkId,
				// 		materielCode: item.materielCode,
				// 		factoryName: this.myFormData[index].factory,
				// 		factoryCode:this.myFormData[index].facCode,
				// 		storageSite: this.myFormData[index].storageSite,
				// 		inputNum: this.myFormData[index].inputNum,
				// 		id: this.myFormData[index].id,
				// 		mobileCode: '105',
				// 	}
				// }
				// this.$http.post('storage/mesCertificatePerk/addCertificateItem', data).then(res => {
				// 	if (res.data.success) {
				// 		uni.hideLoading()
				// 		// this.$tip.success(res.data.message)
    //         this.modal.show({ type:'success', content:res.data.message })
				// 		this.pageNo = 1
				// 		this.list = []
				// 		this.myFormData = []
				// 		this.getListMesCertificateItemByMainId();
				// 	} else {
				// 		// this.$tip.error(res.data.message)
    //         this.modal.show({ type:'error', content:res.data.message })
				// 	}
				// })
			},
			handleRightClick() {
				this.$Router.push({
					name: 'warehouseDetailSaomiao',
					params: {
						isAllScan: true
					}
				})
			},
			// 扫描入库
			WarehousingSaomiao(type, index,item){
				let data = {
					perkId: item.perkId,
					materielCode: item.materielCode,
					reserveCode: item.id,
					mobileCode: '106',
					mobileType: '扫描入库'
				}
				this.$http.post('storage/mesCertificatePerk/scanEnterStore', data).then(res => {
					if (res.data.success) {
						this.$Router.push({
							name: 'warehouseDetailSaomiao',
							params: {
								isAllScan: false
							}
						})
						uni.setStorageSync('scanEnterStoreId',res.data.result.id)
						uni.setStorageSync('warehouserDetailId',item.id)
					} else {
						// this.$tip.error(res.data.message)
            this.modal.show({ type:'error', content:res.data.message })
					}
				})
			}
		},
	}
</script>

<style lang="less">
	page {
		background-color: #f1f1f1;

		.card {
			margin: 30rpx;
			box-shadow: 0px 0px 5rpx 2rpx rgba(0, 0, 0, 0.1);
			border-radius: 10rpx;
			background-color: white;

			.top {
				display: flex;
				align-items: center;
				justify-content: space-between;
				font-weight: bold;
				padding: 20rpx 30rpx;

				.left {
					font-size: 28rpx;
					font-family: PingFang SC;
					font-weight: 400;
					color: #485A75;
					line-height: 50rpx;
				}

				.right {
					font-size: 28rpx;
					font-family: PingFang SC;
					font-weight: 400;
					color: #191919;
					line-height: 50rpx;
				}
			}

			.line {
				width: 750rpx;
				height: 10rpx;
				background: #F3F5F9;
			}

			.bottom {
				padding: 20rpx 30rpx;

				.box {
					display: flex;
					justify-content: space-between;
					padding: 20rpx 0;
					align-items: center;

					.lable {
						color: #485A75;
						font-size: 28rpx;
					}

					.content {
						width: 70%;
						text-align: right;
						color: #191919;
						font-weight: bold;
						word-break: break-all;
					}
				}
			}

			.btns {
				display: flex;
				justify-content: space-between;
				align-items: center;
				padding: 0 20rpx;
				font-size: 25rpx;
				padding: 20rpx 30rpx;

				view {
					width: 180rpx;
					height: 64rpx;
					line-height: 50rpx;
					color: white;
					border-radius: 30rpx;
					padding: 10rpx 20rpx;
					text-align: center;
				}

				.btn1 {
					background-image: radial-gradient(circle, #3F55E9, #5F71ED, #7D8CF0);
				}

				.btn2 {
					background-image: radial-gradient(circle, #716BA9, #362D85, #57509A);
				}
				.btn3 {
					background-image: radial-gradient(circle, #354D9E, #354D9E, #697BB7);
				}
			}
		}
	}

	.none .none1 {
		font-size: 30rpx;
		text-align: center;
		margin-top: 50rpx;
		color: #999999;
	}

	.none2 {
		font-size: 30rpx;
		text-align: center;
		margin-top: 50rpx;
		color: #999999;
	}

	input {
		border: 2rpx solid #F0F0F0;
		height: 60rpx;
		padding: 10rpx;
		border-radius: 10rpx;
		font-size: 20rpx;
		width: 74%;
		text-align: right;
	}
</style>
